/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.persist.dialect;

import java.sql.Types;

/**
 * 
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">FANGFANG ZHAO</a>
 * @since 0.1.0, Feb 5, 2009
 * @version $Id: MySQL5Dialect.java 20 2009-02-10 03:35:10Z
 *          zhaofang123@gmail.com $
 */
public class MySQL5Dialect extends Dialect {
	private static final long serialVersionUID = 9022968517851901342L;

	public MySQL5Dialect() {
		super();

		registerColumnType(FieldType.PRIMARY.getMask(), "char(40)", Types.CHAR);
		registerColumnType(FieldType.REFERENCE.getMask(), "char(40)", Types.CHAR);
		registerColumnType(FieldType.NREFERENCE.getMask(), "char(40)", Types.CHAR);

		registerColumnType(FieldType.INT.getMask(), "int(11)", Types.INTEGER);
		registerColumnType(FieldType.TINY_INT.getMask(), "tinyint(6)", Types.TINYINT);
		registerColumnType(FieldType.SMALL_INT.getMask(), "smallint(6)", Types.SMALLINT);
		registerColumnType(FieldType.DOUBLE.getMask(), "double", Types.DOUBLE);
		registerColumnType(FieldType.DECIMAL.getMask(), "decimal(19, 4)", Types.DECIMAL);

		registerColumnType(FieldType.CHAR.getMask(), "char(1)", Types.CHAR);
		registerColumnType(FieldType.STRING.getMask(), "varchar(%d)", Types.VARCHAR);
		registerColumnType(FieldType.TEXT.getMask(), "varchar(%d)", Types.VARCHAR);

		registerColumnType(FieldType.DATE.getMask(), "date", Types.DATE);
		registerColumnType(FieldType.DATETIME.getMask(), "date", Types.DATE);
		registerColumnType(FieldType.TIMESTAMP.getMask(), "timestamp", Types.TIMESTAMP);

		registerColumnType(FieldType.BOOL.getMask(), "char(1)", Types.CHAR);
		registerColumnType(FieldType.NTEXT.getMask(), "longtext", Types.LONGVARCHAR);
		registerColumnType(FieldType.BINARY.getMask(), "longblob", Types.BLOB);
	}

	@Override
	public String getDialectName() {
		return "mysql5";
	}

	@Override
	public char getClosedQuote() {
		return QUOTED[0];
	}

	@Override
	public char getStartQuote() {
		return QUOTED[1];
	}
	
	@Override
	public String insertLimit(String query, int limit, int offset) {
		return new StringBuilder(query.length() + 12)
			.append(query)
			.append(" limit ").append(limit)
			.append((offset > 0) ? (" offset " + offset) : "").toString();
	}
	
	@Override
	public boolean supportsLimitOffset() {
		return true;
	}
}
